/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package eu.himeros.spellchecker;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.lucene.search.spell.PlainTextDictionary;
import org.apache.lucene.search.spell.SpellChecker;
import org.apache.lucene.search.spell.StringDistance;
import org.apache.lucene.store.NIOFSDirectory;
import org.apache.lucene.store.NoLockFactory;


/**
 *
 * @author federico
 */
public class LuceneSpellChecker {
    static SpellChecker spellchecker;
    static Hashtable<String,SpellChecker> ht=new Hashtable();
    static int _numSuggestions;
    /**
     * @param args the command line arguments
     */
    public static void init(StringDistance sd, int numSuggestions) {
        _numSuggestions=numSuggestions;
        try {
            String prop=System.getProperties().getProperty("eu.himeros.spellcheckers");
            String[] spellcheckerStrs=prop.split("\\|");
            for(String spellcheckerStr:spellcheckerStrs){
                String[] strs=spellcheckerStr.split("@");
                File fileOrDirDict=new File(strs[1]);
                if(fileOrDirDict.isFile()){
                    File fileDict=fileOrDirDict;
                    File dirDict=fileOrDirDict.getParentFile();
                    NIOFSDirectory spellIndexDirectory =new NIOFSDirectory(dirDict, new NoLockFactory());
                    spellchecker = new SpellChecker(spellIndexDirectory);
                    spellchecker.indexDictionary(new PlainTextDictionary(fileDict));
                }else if(fileOrDirDict.isDirectory()){
                    File dirDict=fileOrDirDict;
                    NIOFSDirectory spellIndexDirectory =new NIOFSDirectory(dirDict, new NoLockFactory());
                    spellchecker = new SpellChecker(spellIndexDirectory);
                    spellchecker.setSpellIndex(spellIndexDirectory);
                }
                ht.put(strs[0], spellchecker);
            }
            //AncientGreekDistance sd=new AncientGreekDistance();
            spellchecker.setStringDistance(sd);
        }catch(Exception ex){
            Logger.getLogger(LuceneSpellChecker.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static String[] spellcheck(String word, String lang){
        try{
            spellchecker=ht.get(lang);
            return spellchecker.suggestSimilar(word,_numSuggestions);
        }catch (IOException ex) {
            Logger.getLogger(LuceneSpellChecker.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
//
    public static boolean exist(String word, String lang){
        try{
            spellchecker=ht.get(lang);
            return spellchecker.exist(word);
        }catch(Exception ex){
            Logger.getLogger(LuceneSpellChecker.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
}
